package main.war;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Comparator;
import java.util.TreeMap;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;

import main.war.city.building.CityBuilding;
import main.war.city.building.Loc;

public class BuildingFile {
	private static File rootPath;
	private static BuildingFileIterable st;
	public static void mkdirs(){
		if(rootPath == null)rootPath = new File(CityWar.getPath()+"/Building");
		if(!rootPath.exists())
			rootPath.mkdirs();
	}
	public static boolean saveBuildingInFile(CityBuilding cb){
		mkdirs();
		File file = new File(rootPath,cb.getName()+".bud");
		try {
			file.createNewFile();
			Loc A = cb.getTerritory().getLocA();
			Loc B = cb.getTerritory().getLocB();
			World world = cb.getCity().getWorld();
			StringBuffer sb = new StringBuffer().append("World: "+world.getName()+",Location: ");
			for(int i=0;i<(A.getY()-B.getY());i++){
				sb.append("y: "+i+",");
				for(int o=0;o<(A.getX()-B.getX());o++){
					sb.append("x: "+o+",");
					for(int t=0;t<(A.getZ()-B.getZ());t++){
						ItemStack is = world.getBlockAt(new Location(world,o,i,t)).getState().getData().toItemStack();
						sb.append("z: "+t+", {"+is.getType().toString()+"."+is.getAmount()+"},");
					}
				}
			}
			byte[] b= sb.toString().getBytes("GB2312");
			FileOutputStream fo = new FileOutputStream(file);
			fo.write(b);
			fo.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	public static File getPath(){
		return rootPath;
	}
	/*
	public TreeMap<Loc,ItemStack> getBuildingInFile(File f){
		if(!f.exists())return null;
		if(!f.getPath().endsWith(".bud"))return null;
		try {
			FileInputStream in = new FileInputStream(f);
			byte[] b =new byte[in.available()];
			in.read(b);
			in.close();
			String str = new String(b);
			st = new StringIterable(str);
			TreeMap<Loc,ItemStack> tm = new TreeMap<>(new LocComparator());
			while(st.hasNext()){
				String[] s = st.next();
				int x=0;
				int y=0;
				int z=0;
				World world=null;
				switch(s[0]){
				case "x":{
					x = Integer.parseInt(s[1]);
				}
				case "y":{
					y = Integer.parseInt(s[1]);
				}
				case "z":{
					z = Integer.parseInt(s[1]);
					tm.put(new Loc(world,x,y,z), getItemFormString(s[2]));
				}
				case "World":{
					world = main.getServer().getWorld(s[1]);
				}
				}
			}
			return tm;
		} catch (IOException ex) {
			ex.printStackTrace();
			return null;
		}
		
	}
	*/
	private ItemStack getItemFormString(String s){
		return new ItemStack(Material.getMaterial(s.substring(0, s.indexOf("."))),Integer.parseInt(s.substring(s.indexOf(".")+1)));
	}
	public static String getValue(CityBuilding cb,String key){
		return getValue(new File(rootPath,cb.getName()+".bud"),key);
	}
	public static String getValue(String BuildingName,String key){
		return getValue(new File(rootPath,BuildingName+".bud"),key);
	}
	private static String getValue(File f,String key){
		if(!f.exists())return null;
		try {
			FileInputStream in = new FileInputStream(f);
			byte[] b =new byte[in.available()];
			in.read(b);
			in.close();
			String str = new String(b);
			st = new BuildingFileIterable(str);
			while(st.hasNext()){
				String[] s = st.next();
				if(s[0].equals(key))
					return s[1];
			}
			return null;
		} catch (IOException ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
}
class BuildingFileIterable{
	private StringBuffer sb;
	private int index=0;
	public BuildingFileIterable(String s){
		sb = new StringBuffer(s);
	}
	public boolean hasNext(){
		if(sb.indexOf(":",index)!=-1)return true;
		return false;
	}
	public String[] next(){
		int i = sb.indexOf(": ",index)+2;
		int key;
		if(sb.lastIndexOf(",",i)!=-1){
			key = sb.lastIndexOf(",",i)+1;
		}else {
			key =0;
		}
		int value = sb.indexOf(",",i);
		String s[] = {sb.substring(key,i-2),sb.substring(i,value)};
		this.index=value;
		return s;
	}
}
